package com.ev.custom.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ev.custom.dao.ContentAssocDao;
import com.ev.custom.domain.ContentAssocDO;
import com.ev.custom.service.ContentAssocService;
import com.ev.framework.utils.StringUtils;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ContentAssocServiceImpl implements ContentAssocService {
	@Autowired
	private ContentAssocDao contentAssocDao;

	@Override
	public int removeByAssocIdAndassocType(Long assocId, String type) {
		return contentAssocDao.removeByAssocIdAndassocType(assocId,type);
	}

	@Override
	public ContentAssocDO get(Integer id){
		return contentAssocDao.get(id);
	}
	
	@Override
	public List<ContentAssocDO> list(Map<String, Object> map){
		return contentAssocDao.list(map);
	}

	@Override
	public List<ContentAssocDO> listByAssocId(Long assocId, String type) {
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
		param.put("assocId", assocId);
		param.put("assocType", type);
		return this.list(param);
	}

	@Override
	public List<ContentAssocDO> listByAssocIds(List<Long> assocIds, String type) {
		Map<String, Object> param = Maps.newHashMapWithExpectedSize(2);
		param.put("assocIds", assocIds);
		param.put("assocType", type);
		return this.list(param);
	}

	@Override
	public int count(Map<String, Object> map){
		return contentAssocDao.count(map);
	}
	
	@Override
	public int save(ContentAssocDO contentAssoc){
		return contentAssocDao.save(contentAssoc);
	}
	
	@Override
	public int update(ContentAssocDO contentAssoc){
		return contentAssocDao.update(contentAssoc);
	}
	
	@Override
	public int remove(Integer id){
		return contentAssocDao.remove(id);
	}

	@Override
	public int removeByAssocId(Long assocId){
		return contentAssocDao.removeByAssocId(assocId);
	}

	@Override
	public int batchRemove(Integer[] ids){
		return contentAssocDao.batchRemove(ids);
	}

	@Override
	public void saveList(Long assocerId, String[] paths, String type) {
		removeByAssocIdAndassocType(assocerId,type);
		if(paths != null && paths.length>0){
			for(String path : paths){
				ContentAssocDO contentAssocDO = new ContentAssocDO(assocerId,type,path);
				save(contentAssocDO);
			}
		}
	}

	@Override
	public void saveList(Long assocerId, List<String> paths, String type) {
		if(!ObjectUtils.isEmpty(paths)){
			for(String path : paths){
				ContentAssocDO contentAssocDO = new ContentAssocDO(assocerId,type,path);
				save(contentAssocDO);
			}
		}
	}

	@Override
	public void saveList(Long assocerId, JSONArray pathList, String type) {
		for(int i=0;i<pathList.size();i++){
			JSONObject obj = pathList.getJSONObject(i);
			String fileName = obj.get("fileName").toString();
			String filePath = obj.get("filePath").toString();
			ContentAssocDO contentAssocDO = new ContentAssocDO(assocerId,type,fileName,filePath);
			save(contentAssocDO);
		}
	}

	@Override
	public void saveList(Long assocId, String uploadAttachment, String type, boolean isInsert) {
		if (!isInsert) {
			this.removeByAssocIdAndType(assocId, type);
		}
		if (StringUtils.isNoneBlank(uploadAttachment)) {
			this.saveList(assocId, JSONArray.parseArray(uploadAttachment), type);
		}
	}

	@Override
	public void saveListForEntity(Long assocerId, List<ContentAssocDO> pathList, String type) {
		for(ContentAssocDO contentAssocDO :pathList ){
			contentAssocDO.setAssocId(assocerId);
			contentAssocDO.setAssocType(type);
			save(contentAssocDO);
		}
	}


	@Override
	public void deleteList(String[] ids) {
		if(ids != null && ids.length>0){
			for(String id : ids){
				remove(Integer.parseInt(id));
			}
		}
	}

	@Override
	public int removeByAssocIdAndType(@Size(min = 1)Long[] id,@NotNull String type) {

		Map<String,Object> query =new HashMap<>();
		int[] ids = new int[id.length];

		for (int i = 0; i < id.length; i++) {
			ids[i] = id[i].intValue();
		}
		query.put("assocId",ids);
		query.put("assocType",type);

		return contentAssocDao.removeByAssocIdAndType(query);
	}

	@Override
	public int removeByAssocIdAndType(Long id, String type) {
		Long [] ids = {id};
		return this.removeByAssocIdAndType(ids,type);
	}

	@Override
	public int removeByAssocIdsAndTypes(@Size(min = 1) Long[] id,@Size(min = 1) String[] type) {

		Map<String,Object> query =new HashMap<>();
		int[] ids = new int[id.length];

		for (int i = 0; i < id.length; i++) {
			ids[i] = id[i].intValue();
		}
		query.put("assocId",ids);
		query.put("assocType",type);

		return contentAssocDao.removeByAssocIdsAndTypes(query);
	}


}
